package com.wawu.server.controller;

import com.wawu.common.annotation.IOC.Autowired;
import com.wawu.common.annotation.IOC.component.RestController;
import com.wawu.common.annotation.controller.mapping.GetMapping;
import com.wawu.common.annotation.controller.mapping.PostMapping;
import com.wawu.common.annotation.controller.mapping.RequestMapping;
import com.wawu.common.annotation.controller.parameter.RequestBody;
import com.wawu.common.result.Result;
import com.wawu.pojo.dto.LoginDTO;
import com.wawu.pojo.entity.User;
import com.wawu.pojo.vo.LoginVO;
import com.wawu.pojo.vo.getDataVO;
import com.wawu.server.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
//    @OptionsMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) throws SQLException {
        LoginVO loginVO=userService.login(loginDTO);
        return Result.success(loginVO);
    }

    /**
     * 注册用户（这里就不用dto了，跟数据库表都是对应的）
     * @param user
     * @return
     */
    @PostMapping("/signup")
    public Result<LoginVO> signup(@RequestBody User user) throws SQLException {
        LoginVO loginVO=userService.signup(user);
        return Result.success(loginVO);
    }

    @GetMapping("/data")
    public Result<List<getDataVO>> getUserList(HttpServletRequest request) throws SQLException, InvocationTargetException, IllegalAccessException {
        return Result.success(userService.getdata(request));
    }
}
